package org.esk.dablog.web.forms;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.esk.dablog.model.Entry;
import org.apache.commons.lang.StringUtils;

/**
 * This class implements standard entry validation routines
 * 1. subject should be filled in
 * 2. length of "text" field should not be more than 20KB
 * User: esk
 * Date: 22.12.2006
 * Time: 19:45:37
 * $Id:$
 */
public class EntryValidator implements Validator {
    private static final int MAX_ALLOWED_TEXT_LENGTH = 20000;

    public boolean supports(Class clazz) {
        return Entry.class.isAssignableFrom(clazz);
    }

    public void validate(Object obj, Errors errors) {
        Entry e = (Entry) obj;
        if(StringUtils.isBlank(e.getHeader())) {
            errors.rejectValue("header", "app.entry.header.empty");
        }
        if(e.getText() != null && e.getText().length() >= MAX_ALLOWED_TEXT_LENGTH) {
            errors.rejectValue("text", "app.entry.text.maxlength");            
        }
    }
}
